/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}, tts = false) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}

	form.aiSpeak = workstep.do?.data; // 1
	form.aiSpeakVideo = workstep.do?.video;
	form.intentLib = workstep.intentLib;
	form.waitTime = workstep.after.data?.speechBeginWaitingTimeMS; // 2

	// other
	if (workstep.answer[0]?.after) {
		form.wordsOther = workstep.answer[0]?.do?.data; // 3
		form.wordsOtherVideo = workstep.answer[0]?.do?.video;
		form.wordsOverTime = workstep.answer[0]?.after?.data?.speechBeginWaitingTimeMS; // 4
	} else {
		form.wordsOverTime = workstep.answer[0]?.do?.data?.speechBeginWaitingTimeMS; // 4
		form.wordsOtherVideo = workstep.answer[0]?.do?.video;
	}
	if (workstep.errorRemind?.after) {
		form.wordsEnd = workstep.errorRemind?.do?.data; // 6
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
		form.nextStepOther = workstep.errorRemind?.after?.id;
	} else {
		form.nextStepOther = workstep.errorRemind?.do?.id;
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
	}
	form.otherTimesMax = workstep.errorRemind?.maximum; // 5

	// noAnswer
	if (workstep.answer[1]?.after) {
		form.wordsNoAnswer = workstep.answer[1]?.do?.data; // 7
		form.wordsNoAnswerVideo = workstep.answer[1]?.do?.video;
		form.noAnswerOverTime = workstep.answer[1]?.after?.data?.speechBeginWaitingTimeMS; // 4
	} else {
		form.noAnswerOverTime = workstep.answer[1]?.do?.data?.speechBeginWaitingTimeMS; // 4
		form.wordsNoAnswerVideo = workstep.answer[1]?.do?.video;
	}

	if (workstep.emptyErrorRemind?.after) {
		form.nextStepNoAnswer = workstep.emptyErrorRemind?.after?.id;
		form.wordsEndNoAnswer = workstep.emptyErrorRemind?.do?.data; // 10
		form.wordsEndNoAnswerVideo = workstep.emptyErrorRemind?.do?.video;
	} else {
		form.nextStepNoAnswer = workstep.emptyErrorRemind?.do?.id;
		form.wordsEndNoAnswerVideo = workstep.emptyErrorRemind?.do?.video;
	}

	form.noAnswerTimesMax = workstep.emptyErrorRemind?.maximum; // 9

	// ivvrOption
	form.ivvrOptions = [];
	workstep.answer.map(item => {
		if (item.type.includes('ivvr')) {
			let option = {};
			if (!item.isNluPoint) {
				option.answerIntention = item.key;
			} else {
				option.answerIntentionSelect = item.key;
			}
			console.log(item);

			option.answerSpeak = item.after ? item.do?.data : '';
			option.video = item.after ? item.do.video : tts ? item.do.video : null;
			option.nextStepsAnswer = item.after ? item.after.id : item.do.id;
			option.checkedAnswer = item.isNluPoint;
			option.name = item.name;
			option.answerSpeakVariables = item.after ? item.do.variables : tts ? item.do.variables : [];
			form.ivvrOptions.push(option);
		}
	});
	// 智能问答
	form['intelligentReply'] = workstep.intelligentReply;
	form['aiProcessSpeech'] = workstep.intelligentReply.mainProcessSpeech;

	/** 变量存储处理开始 */
	form['aiSpeakVariableId'] = workstep.do.variableId;
	form['aiSpeakVariables'] = workstep.do.variables?.filter(item => form['aiSpeak']?.indexOf(`{{${item.name}}}`) !== -1);
	form['aiProcessSpeechVariableId'] = workstep.intelligentReply.variableId;
	form['aiProcessSpeechVariables'] = workstep.intelligentReply.variables?.filter(item => form['aiProcessSpeech']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsOtherVariableId'] = workstep.answer[0].do.variableId;
	form['wordsOtherVariables'] = workstep.answer[0].do.variables?.filter(item => form['wordsOther']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsNoAnswerVariableId'] = workstep.answer[1].do.variableId;
	form['wordsNoAnswerVariables'] = workstep.answer[1].do.variables.filter(item => form['wordsNoAnswer']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndVariableId'] = workstep.errorRemind.do.variableId;
	form['wordsEndVariables'] = workstep.errorRemind.do.variables?.filter(item => form['wordsEnd']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndNoAnswerVariableId'] = workstep.emptyErrorRemind.do.variableId;
	form['wordsEndNoAnswerVariables'] = workstep.emptyErrorRemind.do.variables?.filter(item => form['wordsEndNoAnswer'].indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	form.name = workstep.name;
};
InitModalMethod['ivvr'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}, tts = false) => {
	console.log('ivvr parseModalData', form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}

	workstep.do.data = form.aiSpeak; // 1
	workstep.do.video = form.aiSpeakVideo;
	workstep.intentLib = form.intentLib;

	workstep.after.data.speechBeginWaitingTimeMS = form.waitTime; // 2

	// 智能问答
	workstep.intelligentReply = form['intelligentReply'];
	// workstep.intelligentReplies = form['intelligentReplies'];

	workstep.answer = [
		{
			do: {
				data: '',
				command: 'speak',
				variableId: '',
				variables: []
			},
			key: 'others',
			type: 'other',
			after: {
				data: {
					judgmentType: 'positive-negative',
					speechBeginWaitingTimeMS: 3000
				},
				command: 'listen'
			}
		},
		{
			key: 'empty',
			do: {
				data: '',
				command: 'speak',
				variableId: '',
				variables: []
			},
			after: {
				data: {
					judgmentType: 'positive-negative',
					speechBeginWaitingTimeMS: 3000
				},
				command: 'listen'
			},
			type: 'empty'
		}
	];
	// other
	if ((form.wordsOther && !tts) || (form.wordsOtherVideo && !tts) || (tts && form.wordsOther && form.wordsOtherVideo)) {
		workstep.answer[0].do.data = form.wordsOther; // 4
		workstep.answer[0].do.video = form.wordsOtherVideo;
		workstep.answer[0].after.data.speechBeginWaitingTimeMS = form.wordsOverTime; // 4
	} else {
		workstep.answer[0].do = Object.assign({}, workstep.answer[0].after);
		workstep.answer[0].do.data.speechBeginWaitingTimeMS = form.wordsOverTime; // 4
		workstep.answer[0].do.video = form.wordsOtherVideo;
		delete workstep.answer[0].after;
	}
	if ((form.wordsEnd && !tts) || (form.wordsEndVideo && !tts) || (tts && form.wordsEnd && form.wordsEndVideo)) {
		workstep.errorRemind.after.id = form.nextStepOther;
		workstep.errorRemind.do.data = form.wordsEnd; // 5
		workstep.errorRemind.do.video = form.wordsEndVideo;
	} else {
		workstep.errorRemind.do = Object.assign({}, workstep.errorRemind.after);
		workstep.errorRemind.do.id = form.nextStepOther;
		workstep.errorRemind.do.video = form.wordsEndVideo;
		delete workstep.errorRemind.after;
	}
	workstep.errorRemind.maximum = form.otherTimesMax; // 5

	// noAnswer
	if ((form.wordsNoAnswer && !tts) || (form.wordsNoAnswerVideo && !tts) || (tts && form.wordsNoAnswer && form.wordsNoAnswerVideo)) {
		workstep.answer[1].do.data = form.wordsNoAnswer; // 7
		workstep.answer[1].do.video = form.wordsNoAnswerVideo;
		workstep.answer[1].after.data.speechBeginWaitingTimeMS = form.noAnswerOverTime; // 4
	} else {
		workstep.answer[1].do = Object.assign({}, workstep.answer[1].after);
		workstep.answer[1].do.data.speechBeginWaitingTimeMS = form.noAnswerOverTime; // 4
		workstep.answer[1].do.video = form.wordsNoAnswerVideo;
		delete workstep.answer[1].after;
	}

	if ((form.wordsEndNoAnswer && !tts) || (form.wordsEndNoAnswerVideo && !tts)) {
		workstep.emptyErrorRemind.after.id = form.nextStepNoAnswer;
		workstep.emptyErrorRemind.do.data = form.wordsEndNoAnswer; // 10
		workstep.emptyErrorRemind.do.video = form.wordsEndNoAnswerVideo;
	} else {
		workstep.emptyErrorRemind.do = Object.assign({}, workstep.emptyErrorRemind.after);
		workstep.emptyErrorRemind.do.id = form.nextStepNoAnswer;
		workstep.emptyErrorRemind.do.video = form.wordsEndNoAnswerVideo;
		delete workstep.emptyErrorRemind.after;
	}
	// ivvrOption
	let index = 0;
	form.ivvrOptions.map(item => {
		index++;
		let option = { type: `ivvr${index}`, isNluPoint: item.checkedAnswer, key: item.answerIntention, name: item.name };
		if (item.checkedAnswer) {
			option.key = item.answerIntentionSelect;
		}
		if ((item.answerSpeak && !tts) || (item.video && !tts) || (tts && item.answerSpeak && item.video)) {
			option.do = { data: item.answerSpeak, command: 'speak', variables: [] };
			option.do.video = item.video;
			option.after = { command: 'jump-to', id: item.nextStepsAnswer, no: item.nextStepsAnswer };
		} else {
			option.do = { command: 'jump-to', id: item.nextStepsAnswer, no: item.nextStepsAnswer };
			option.do.video = item.video;
		}
		workstep.answer.push(option);
	});
	workstep.emptyErrorRemind.maximum = form.noAnswerTimesMax; // 9

	/** 变量存储处理开始 */
	workstep.do.variableId = form['aiSpeakVariableId'];
	workstep.do.variables = form['aiSpeakVariables']?.filter(item => form['aiSpeak'].indexOf(`{{${item.name}}}`) !== -1);
	workstep.intelligentReply.variableId = form['aiProcessSpeechVariableId'];
	if (form['aiProcessSpeech']) {
		workstep.intelligentReply.variables = form['aiProcessSpeechVariables']?.filter(item => form['aiProcessSpeech'].indexOf(`{{${item.name}}}`) !== -1);
	}

	workstep.answer[0].do.variableId = form['wordsOtherVariableId'];
	workstep.answer[0].do.variables = form['wordsOtherVariables']?.filter(item => form['wordsOther'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[1].do.variableId = form['wordsNoAnswerVariableId'];
	workstep.answer[1].do.variables = form['wordsNoAnswerVariables']?.filter(item => form['wordsNoAnswer'].indexOf(`{{${item.name}}}`) !== -1);
	workstep.errorRemind.do.variableId = form['wordsEndVariableId'];
	workstep.errorRemind.do.variables = form['wordsEndVariables']?.filter(item => form['wordsEnd'].indexOf(`{{${item.name}}}`) !== -1);
	workstep.emptyErrorRemind.do.variableId = form['wordsEndNoAnswerVariableId'];
	workstep.emptyErrorRemind.do.variables = form['wordsEndNoAnswerVariables']?.filter(item => form['wordsEndNoAnswer'].indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */
	workstep.name = form.name;
};
ParseModalMethod['ivvr'] = parseModalData;

// {
//     "robotProperty": {
//         "needHearing": true,
//         "videoType": "",
//         "audioType": ""
//     },
//     "name": "",
//     "do": {
//         "data": "1",
//         "command": "speak",
//         "variables": [],
//         "variableId": ""
//     },
//     "id": "ivvr_42",
//     "no": "ivvr_42",
//     "type": "ivvr",
//     "after": {
//         "data": {
//             "judgmentType": "positive-negative",
//             "speechBeginWaitingTimeMS": 2
//         },
//         "command": "listen"
//     },
//     "intelligentReply": {
//         "isOn": false,
//         "data": [],
//         "mainProcessSpeech": "",
//         "speechBeginWaitingTimeMS": 2,
//         "variables": [],
//         "variableId": ""
//     },
//     "intentLib": "ivvr-presentation",
//     "answer": [
//         {
//             "do": {
//                 "data": "3",
//                 "command": "speak",
//                 "variableId": "",
//                 "variables": []
//             },
//             "key": "others",
//             "type": "other",
//             "after": {
//                 "data": {
//                     "judgmentType": "positive-negative",
//                     "speechBeginWaitingTimeMS": 4
//                 },
//                 "command": "listen"
//             }
//         },
//         {
//             "key": "empty",
//             "do": {
//                 "data": "7",
//                 "command": "speak",
//                 "variableId": "",
//                 "variables": []
//             },
//             "after": {
//                 "data": {
//                     "judgmentType": "positive-negative",
//                     "speechBeginWaitingTimeMS": "8"
//                 },
//                 "command": "listen"
//             },
//             "type": "noAnswer"
//         }，
// {
//     "do": {
//         "command": "jump-to",
//         "id": "BlankNode_3",
//         "no": "BlankNode_3"
//     },
//     "key": "insurance_categories",
//     "isNluPoint": true,
//     "type": "ivvr1",
//     "after": {},
//     "name": "咨询险种"
// }
//     ],
//     "errorRemind": {
//         "do": {
//             "data": "6",
//             "command": "speak",
//             "variables": [],
//             "variableId": ""
//         },
//         "after": {
//             "command": "jump-to",
//             "id": "BlankNode_43",
//             "no": "BlankNode_43"
//         },
//         "maximum": 5
//     },
//     "emptyErrorRemind": {
//         "do": {
//             "data": "10",
//             "command": "speak",
//             "variables": [],
//             "variableId": ""
//         },
//         "after": {
//             "command": "jump-to",
//             "id": "BlankNode_44",
//             "no": "BlankNode_44"
//         },
//         "maximum": 9,
//         "type": "noAnswers"
//     },
//     "completed": true,
//     "hasStartNode": false
// }
